package com.example.foodisimportant.LoginActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.foodisimportant.utils.DBUtil;
import com.example.foodisimportant.R;
import com.example.foodisimportant.RegisterActivity.RegisterTest;
import com.example.foodisimportant.StudentViewActivity.StudentViewActivity;

public class LoginActivity extends AppCompatActivity {
    private Button mBtnLogin;
    private TextView mRegister;
    private EditText mUsername;
    private EditText mPassward;
    private CheckBox mRememberPwd;
    private String Username,Passward;
    private Handler handler; //Handler主要用于异步消息的处理，也就是发送消息和接收消息不是同步的处理。 这种机制通常用来处理相对耗时比较长的操作。
    private SharedPreferences preferences;//用来进行数据的存储和读取
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        //找到控件
        mBtnLogin = findViewById(R.id.btn_lg);
        mRegister=findViewById(R.id.tv_rg);
        mUsername=findViewById(R.id.et1);
        mPassward=findViewById(R.id.et2);
        mRememberPwd=findViewById(R.id.cb1);
        preferences= PreferenceManager.getDefaultSharedPreferences(this);
        //存放登录信息
        boolean isRemember=preferences.getBoolean("mRememberPwd",false);
        if (isRemember){
            Username=preferences.getString("name","");
            Passward=preferences.getString("password","");
            mUsername.setText(Username);
            mPassward.setText(Passward);
            mRememberPwd.setChecked(true);
        }
        //子线程
        WorkThread wt1=new WorkThread();
        wt1.start();
        //登录点击事件
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //对应值
                Username=mUsername.getText().toString();
                Passward=mPassward.getText().toString();
                editor = preferences.edit();
                //判断输入是否为空
                if(Username.isEmpty() || Passward.isEmpty()){
                    Toast.makeText(LoginActivity.this, "请输入正确的用户名/账号和密码！", Toast.LENGTH_SHORT).show();
                }else {
                    //提取记住密码的值
                    if(mRememberPwd.isChecked()){
                        editor.putBoolean("mRememberPwd",true);
                        editor.putString("name",Username);
                        editor.putString("password",Passward);
                    }else{editor.clear();}
                    editor.apply();
                    Message m= handler.obtainMessage();
                    Bundle b=new Bundle();
                    b.putString("name",Username);
                    b.putString("pass",Passward);
                    m.setData(b);
                    handler.sendMessage(m);
                }
            }
        });

        //注册跳转
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent(LoginActivity.this, RegisterTest.class);//跳转到注册界面
                startActivity(intent);
            }
        });
    }

    class WorkThread extends Thread{
        @Override
        public void run() {
            Looper.prepare();
            handler=new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Bundle b = msg.getData();//得到与信息对用的Bunlde，Bunlde的作用是在Activity间传递数据
                    DBUtil db = new DBUtil(Username,Username, Passward,null);//调用数据库查询类
                    String ret = db.QuerySQL(Username, Passward);//得到返回值
                    if (ret.equals("1")) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this, StudentViewActivity.class);
                        startActivity(intent);
                        finish();
                        return;
                    }else {
                        Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            };
            Looper.loop();//Looper循环，通道中有数据执行，无数据堵塞
        }
    }

}